#include "form_setting.h"
#include "ui_form_setting.h"

// add necessary includes here
#include "utils/configure/configure_bll.h"


/*
 * Function:    FormSetting
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormSetting::FormSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormSetting)
{
    qDebug() << Q_FUNC_INFO;
    ui->setupUi(this);
}

/*
 * Function:    ~FormSetting
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormSetting::~FormSetting()
{
    qDebug() << Q_FUNC_INFO;
    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormSetting" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormSetting" class
 */
FormSetting* FormSetting::getInstance(QWidget *pParent)
{
    static FormSetting *pNew = nullptr;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormSetting(pParent);
        if(nullptr == pNew) return nullptr;

        // After constructing object, initialize the object
        pNew->_initialze(); 

        // Init the window style
        pNew->_initUi();
    }

    // Set the parent object
    if(pParent) pNew->setParent(pParent);

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormSetting::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    ui->pushButton_Mode->setText(" \n\n\n" + tr("Mode"));
    ui->pushButton_Language->setText(" \n\n\n" + tr("Language"));
    ui->pushButton_Datetime->setText(" \n\n\n" + tr("Datetime"));
    ui->pushButton_Network->setText(" \n\n\n" + tr("Network"));
    ui->pushButton_Log->setText(" \n\n\n" + tr("Log"));
    ui->pushButton_Version->setText(" \n\n\n" + tr("Version"));
    ui->pushButton_Upgrade->setText(" \n\n\n" + tr("Upgrade"));
    ui->pushButton_DataTools->setText(" \n\n\n" + tr("Data Tools"));

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI()), this, SLOT(slotRefreshUI()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormSetting::_initUi()
{
    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/setting/%2.qss").arg("skin_1").arg(this->objectName()));

    // Set the property of buttons
    foreach (auto var, QList<QPushButton*>()
             << ui->pushButton_Mode
             << ui->pushButton_Language
             << ui->pushButton_Datetime
             << ui->pushButton_Network
             << ui->pushButton_Log
             << ui->pushButton_Version
             << ui->pushButton_Upgrade
             << ui->pushButton_DataTools) {
        var->setFocusPolicy(Qt::NoFocus);
    }
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		void
* Return:		void
*/
void FormSetting::slotRefreshUI()
{
    ui->pushButton_Upgrade->setVisible(false);
    ui->pushButton_DataTools->setVisible(false);

    // Get the login mode
    ST_CFG_LOGIN_MODE stLoginMode;
    ConfigureBLL::getInstance(this)->loadLoginMode(stLoginMode);

    switch ((int)stLoginMode.eumLoginMode) {
    case E_MODE_ADMIN:
        ui->pushButton_Upgrade->setVisible(true);
        ui->pushButton_DataTools->setVisible(true);
        break;
    case E_MODE_FACTORY:
        ui->pushButton_Upgrade->setVisible(true);
        ui->pushButton_DataTools->setVisible(true);
        break;
    default: break;
    }
}

void FormSetting::on_pushButton_Mode_clicked()
{
    // Show the mode window
    emit signalShowPageMode();
}

void FormSetting::on_pushButton_Language_clicked()
{
    // Show the language page
    emit signalShowPageLanguage();
}

void FormSetting::on_pushButton_Datetime_clicked()
{
    // Show the datetime page
    emit signalShowPageDatetime();
}

void FormSetting::on_pushButton_Network_clicked()
{
    // Show the network window
    emit signalShowPageNetwork();
}

void FormSetting::on_pushButton_Log_clicked()
{
    // Show the log setting window
    emit signalShowPageLog();
}

void FormSetting::on_pushButton_Version_clicked()
{
    // Show the version page
    emit signalShowPageVersion();
}

void FormSetting::on_pushButton_Upgrade_clicked()
{
    // Show the upgrade page
    emit signalShowPageUpgrade();
}

void FormSetting::on_pushButton_DataTools_clicked()
{
    // Show the data tools window
    emit signalShowPageDataTools();
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *e - the pointer of "QEvent"
* Return:		void
*/
void FormSetting::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        qDebug() << Q_FUNC_INFO;
        ui->retranslateUi(this);

        ui->pushButton_Mode->setText(" \n\n\n" + tr("Mode"));
        ui->pushButton_Language->setText(" \n\n\n" + tr("Language"));
        ui->pushButton_Datetime->setText(" \n\n\n" + tr("Datetime"));
        ui->pushButton_Network->setText(" \n\n\n" + tr("Network"));
        ui->pushButton_Log->setText(" \n\n\n" + tr("Log"));
        ui->pushButton_Version->setText(" \n\n\n" + tr("Version"));
        ui->pushButton_Upgrade->setText(" \n\n\n" + tr("Upgrade"));
        ui->pushButton_DataTools->setText(" \n\n\n" + tr("Data Tools"));

        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}
